package com.question.java.json.serialization;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: Jun Yang
 * @Time: 2018/11/21 上午10:40
 * @Version: 1.0
 */
public class JsonKitMain {
    public static void main(String[] args) {
        // to json
        User user = new User();
        user.setUsername("Joe");
        user.setPassword("123456");
        user.setAge(30);
        user.setBirthday(LocalDate.now());
        String json = JsonKit.toJson(user);
        System.out.println(json);

        List<User> users = new ArrayList<>();
        users.add(user);
        User user2 = new User();
        user2.setUsername("Tom");
        user2.setPassword("123456");
        user2.setAge(30);
        user2.setBirthday(LocalDate.now().plusYears(1));
        users.add(user2);

        System.out.println(JsonKit.toJson(users));

        // from json
        String jsonString = "{\"username\":\"Joe\",\"password\":\"123456\",\"age\":30," +
                "\"birthday\":{\"year\":2018,\"month\":11,\"day\":21}}";
        User userObj = JsonKit.fromJson(jsonString, User.class);
        System.out.println(userObj);

        String jsonList = "[{\"username\":\"Joe\",\"password\":\"123456\",\"age\":30," +
                "\"birthday\":{\"year\":2018,\"month\":11,\"day\":21}}," +
                "{\"username\":\"Tom\",\"password\":\"123456\",\"age\":30," +
                "\"birthday\":{\"year\":2019,\"month\":11,\"day\":21}}]";
        List<User> userList = JsonKit.fromJsonToList(jsonList, ArrayList.class);
        System.out.println(userList);

        String jsonStr = null;
        List<User> userList2 = JsonKit.fromJson(jsonStr, ArrayList.class);

        System.out.println(userList2);

    }
}
